<?php
/**
 * Created by PhpStorm.
 * User: guofeng
 * Date: 2016/3/24 0024
 * Time: 下午 2:47
 */

namespace common\widgets;


use common\helper\Dump;
use yii\base\Widget;
use yii\helpers\Html;
use common\models\CatChapter;
use common\models\CatKnowledge;

class VolumeMenuWidget extends Widget
{
    public $message;
    public $where;
    public $modelName;
    public $list = [];

    public function init()
    {
        parent::init();
//        Dump::p($this->where);
        switch ($this->modelName){
            case "Catchapter":
                $this->list = CatChapter::getVolume($this->where);
                break;
            case "Catknowledge":
                $this->list = CatKnowledge::getVolume($this->where);
                break;
            default:
                $this->list = null;
        }
    }


    public function getVolumeMenuStr($menu)
    {
        $str = '';
        if(empty($menu)){
            return false;
        }
        foreach ($menu as $key => $value) {
            $str .= '<li class="dd-item" data-id="' . $value['id'] . '">';
            $str .= '<div class="dd-handle">';
            $str .= '<div class="ibox-tools dd-nodrag">';
            $str .= '  <a class="" href="add.html?parent_id=' . $value['id'] . '"> 添加 </a>';
            $str .= '  <a class="dropdown-toggle" href="update.html?id=' . $value['id'] . '"> 修改 </a>';
            $str .= '  <a class="link closeLink" data-action="link" data-value="' . $value['id'] . '" href="#"> 删除 </a>';
            $str .= '</div>';
            $str .= '<span class="label label-info"></span>' . $value['title'];
            $str .= '</div>';
            if (isset($value['children'])) {
                $str .= '<ol class="dd-list">';
                $str .= $this->getVolumeMenuStr($value['children']);
                $str .= '</ol>';
            }
            $str .= '</li>';
        }
        return $str;
    }


    public function run()
    {
        $str = $this->getVolumeMenuStr($this->list);
        return Html::decode($str);
    }
}